package com.hzqy.service.activity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.hzqy.persistence.dao.activity.ActivityVoteDao;
import com.hzqy.service.core.GenericServiceBean;
import com.hzqy.web.vo.activity.ActivityVoteUserVo;

@Service
public class ActivityVoteServiceBean extends GenericServiceBean<ActivityVoteUserVo,ActivityVoteDao, Integer> {
	protected final Logger LOG = LoggerFactory.getLogger(getClass());
	@Resource
	ActivityVoteDao activityVoteDao;
	@Override
	public ActivityVoteDao getEntityDao() {
		return activityVoteDao;
	}
	
	public int insertVoteUser(ActivityVoteUserVo voteVo) {
		return activityVoteDao.insertVoteUser(voteVo);
	}
	
	public int updateGroupVoteNum(ActivityVoteUserVo voteVo) {
		return activityVoteDao.updateGroupVoteNum(voteVo);
	}
	
	public List<ActivityVoteUserVo> selectVote(ActivityVoteUserVo voteVo) {
		List<ActivityVoteUserVo> voteList = activityVoteDao.selectVote(voteVo);
		if(voteList==null)
			voteList = new ArrayList<ActivityVoteUserVo>();
		return voteList;
	}
	
	public List<ActivityVoteUserVo> selectVoteGroupByCatalogId(ActivityVoteUserVo voteVo) {
		List<ActivityVoteUserVo> voteList = activityVoteDao.selectVoteGroupByCatalogId(voteVo);
		if(voteList==null)
			voteList = new ArrayList<ActivityVoteUserVo>();
		return voteList;
	}

	public int selectTicketNum(String password, String serverid) {
		Map map=new HashMap();
		map.put("password", password);
		map.put("serverid", serverid);
		return activityVoteDao.selectTicketNum(map);
	}

	public int prvingContentDate(ActivityVoteUserVo voteVo) {
		int totleNum = 0;
		int voteNum = 0;
		int flag = 0;
		List<ActivityVoteUserVo> voteList = activityVoteDao.selectVote(voteVo);
		if(voteList != null) {
			for(int i = 0; i < voteList.size(); i++) {
				if(voteList.get(i).getF_avu_type()==2) { //投票总数
					voteNum = voteList.get(i).getF_avu_votenum();
				} else { //所得总票数
					totleNum += voteList.get(i).getF_avu_votenum();
				}
			}
			if((totleNum-voteNum-voteVo.getF_avu_votenum())>=0) {
				flag = activityVoteDao.prvingContentDate(voteVo.getF_avu_contentid());
			} else {
				flag = 2;
				LOG.error(voteVo.getF_avu_serverid()+"用户投票数:"+voteVo.getF_avu_votenum()+"已超出用户最大所得票数totleNum="+totleNum+"=voteNum="+voteNum);
			}
		} else {
			LOG.error(voteVo.getF_avu_serverid()+"=未查到用户得票数voteList="+voteList);
		}
		return flag;
	}

	public int selectUserOrderVoteNumList(String serverid, String productList) {
		if(productList.endsWith(","))
			productList = productList.substring(0, productList.length()-1);
		HashMap<String,String> map=new HashMap<String,String>();
		map.put("serverid", serverid);
		map.put("productList", productList);
		List<ActivityVoteUserVo> voteUserVoList= activityVoteDao.selectUserOrderVoteNumList(map);
		
		String[] strlist=productList.split(",");
		ArrayList<Integer> productArray = new ArrayList<Integer>();
		for(int i = 0; i < strlist.length; i++) {
			if(strlist[i].length()>=4) {
				try {
					productArray.add(Integer.parseInt(strlist[i]));
				} catch(Exception e) {
					LOG.error("广场舞产品鉴权转换错误。");
				}
			}
		}
		//循环判断数据库中是否已经赠送了票
		if(voteUserVoList!=null){
			for(int i=0;i<voteUserVoList.size();i++){
				for(int j = 0; j < productArray.size(); j++) {
					if(voteUserVoList.get(i).getF_avu_votenum()==productArray.get(j)) {
						productArray.remove(j);
					}
				}
			}
		}
		//将用户定购了未赠送票全部送出
		int voteNum = 0;
		if(productArray.size()>0){
			for(int i = 0; i < productArray.size(); i++) {
				ActivityVoteUserVo voteUserVo=new ActivityVoteUserVo();
				voteUserVo.setF_avu_activityid("0");
				voteUserVo.setF_avu_serverid(serverid);
				voteUserVo.setF_avu_source(0);
				voteUserVo.setF_avu_contentid(0);
				voteUserVo.setF_avu_votenum(productArray.get(i));
				voteUserVo.setF_avu_type(1);
				voteUserVo.setF_avu_memo("order_wxsystem_ticket");
				activityVoteDao.insertVoteUser(voteUserVo);
				voteNum += productArray.get(i);
			}
		}
		return voteNum;
	}
}
